<template>
  <el-select v-model="selectRoles" style="width: 100%" multiple placeholder="请选择角色" @change="onchange">
    <el-option
      v-for="(item,index) in data"
      :key="index"
      :label="item.roleName"
      :value="item.id"/>
  </el-select>
</template>

<script>
import { getRoles } from '@/api/user'
export default {
  props: {
    roles: {
      type: String,
      defalut: ''
    }
  },
  data() {
    return {
      data: [],
      selectRoles: []
    }
  },
  watch: {
    roles(roles) {
      if (roles.index(',') > 0) {
        this.selectRoles = roles.split(',')
      } else if (roles) {
        this.selectRoles.push(roles)
      }
    }
  },
  created() {
    getRoles().then(res => {
      this.data = res
    })
  },
  methods: {
    onchange(value) {
      this.$emit('update:roles', value.toString())
    }
  }
}
</script>

<style scoped>

</style>
